package work.sajor.wechatpush.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import work.sajor.wechatpush.util.HttpUtil;

/**
 * @ClassName Tianqi
 * @Description TODO
 * @Author ydzhao
 * @Date 2022/8/2 16:45
 */
@Service
public class Tianqi {

    @Value("${weather.ak}")
    private String ak;

    @Value("${weather.district_id}")
    private String district_id;

    public static JSONObject getNanjiTianqi() {
        String result = null;
        JSONObject today = new JSONObject();
        try {
//            result = HttpUtil.getUrl("https://api.map.baidu.com/weather/v1/?district_id=" + district_id + "&data_type=all&ak=" + ak);
            //抓包获取:
            result = HttpUtil.getUrl("https://weather.cma.cn/api/now/58321");
            JSONObject jsonObject = JSONObject.parseObject(result);
            JSONObject data = jsonObject.getJSONObject("data").getJSONObject("now");
            //温度:
            today.put("temperature", data.getString("temperature"));
            //湿度:
            today.put("humidity", data.getString("humidity"));
            //风向:
            today.put("windScale", data.getString("windDirection") + " " + data.getString("windSpeed") + "级");


        } catch (Exception e) {
            e.printStackTrace();
        }
        return today;
    }

    public static void main(String[] args) {
        JSONObject nanjiTianqi = getNanjiTianqi();
    }
}
